package com.tjshop.android;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.tjshop.android.adapter.AddressListAdapter;
import com.tjshop.android.adapter.CartGoodsListAdapter;
import com.tjshop.android.constants.UrlConfigs;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.model.tj.json.Address;
import com.tjshop.android.util.SharedPreferencesUtil;
import com.tjshop.android.util.WaitProgressDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
public class MyAddressActivity extends BaseActivity implements OnClickListener {


	EditText input_username, input_pass, input_email, input_tel;
	Button add;
	List<Address> address = new ArrayList<Address>();
	ListView adddressListView;
	AddressListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_myaddress);
		setTheme(R.style.Theme_Sherlock);
		updateTextViewBar("我的地址","新增地址",new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(getApplicationContext(), AddAddressActivity.class);
				Bundle b = new Bundle();
				b.putString("type", "add");
				i.putExtras(b);
				startActivity(i);
			}
		});
		
		adddressListView = (ListView) findViewById(R.id.lv_address);
		add = (Button) findViewById(R.id.add);
		add.setOnClickListener(this);
	}


	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 1:
			findViewById(R.id.noAddress).setVisibility(View.VISIBLE);
			findViewById(R.id.lv_address).setVisibility(View.GONE);
			break;
		case 2:
			findViewById(R.id.noAddress).setVisibility(View.GONE);
			findViewById(R.id.lv_address).setVisibility(View.VISIBLE);
			adapter = new AddressListAdapter(getApplicationContext(), address);
			adddressListView.setAdapter(adapter);
			
			break;
		case 3:
			findViewById(R.id.noAddress).setVisibility(View.VISIBLE);
			findViewById(R.id.lv_address).setVisibility(View.GONE);
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	protected void onResume() {
		getAddress();
		super.onResume();
	}

	private void getAddress() {
		StringRequest stringRequest = new StringRequest(Method.GET,
				UrlConfigs.URL_GET_ADDRESS + "?t=" + GlobeVars.token,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						JSONObject jsonU;
						try {
							jsonU = new JSONObject(response);
							if (jsonU.getInt("r") == 0) {
								JSONArray array = jsonU.getJSONArray("s");
								for(int i = 0;i<array.length();i++){
									JSONObject obj = array.getJSONObject(i);
									Address a = new Address();
									a.parse(obj);
									address.add(a);
								}
								Message message = new Message();
								message.what = 2;
								mHandler.sendMessage(message);
							} else {
								Message message = new Message();
								message.what = 3;
								message.obj = jsonU.getString("m");
								mHandler.sendMessage(message);
							}
						} catch (JSONException e) {
							Message message = new Message();
							message.what = 1;
							message.obj = "服务器返回异常！";
							mHandler.sendMessage(message);
							e.printStackTrace();
						}}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Message message = new Message();
						message.what = 1;
						mHandler.sendMessage(message);

					}

				});

		mQueue.add(stringRequest);
	}


	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
}
